//import JM from '@/common/crypto/crypto.js'
//import JM from '@/common/tools.js'
const is_jm = 0;
// 请求接口
const commoneUrl = "";
//const commoneUrl = "http://47.243.132.19:8091";
function sync_req(json) {
	let url = json.url;
	let data = json.data || '';
	let method = json.method || 'GET';
	let header = json.header || '';
	let success = json.success;
	let fail = json.fail || 'console.log';
	let token =  uni.getStorageSync('token') ? uni.getStorageSync('token') : ''

    let lang = uni.getLocale() || 'en';
	let myheader = {
         'app-id' : 'wx70aa26b5d0445642',
		 'Content-Type': 'application/json',
		 'Connection': 'keep-alive',
		 'lang' : lang,
		// 'Content-Type': 'application/x-www-form-urlencoded',
		 'token': token
	};
	if(method == 'POST'){
		// myheader['Content-Type'] = 'application/x-www-form-urlencoded';
	}
	
	if (header) {
		Object.assign(myheader, header);
	}
	uni.request({
		url: url, //仅为示例，并非真实接口地址。
		data: data,
		method: method,
		header: myheader,
		success: (res) => {	
			let data = null;
			 data = res;
			 if(typeof data!=='object'){
				 console.log('网络请求出错'+url);
				 return false;    
			 }
			 if(data.code == 401){
				uni.showToast({
					title :  this.$t('system.tip4')||'请先登录',
					duration:1500,
					mask:false,
					icon:'none'
				});
				uni.$emit('auto_logout');   
				
			 }				   
			success(data);	
		},
		fail: (err) => {
			fail(err);
		}
	});
}


//get请求封装
function getRequest(url, data,tips='加载中') {
	 // console.log(commoneUrl + url)
	var promise = new Promise((resolve, reject) => {
		var postData = data;
       let token = uni.getStorageSync('token') ? uni.getStorageSync('token') : '';
	   let lang = uni.getLocale() || 'en';
  //  console.log(token)
		uni.request({
			url: commoneUrl + url,
			data: postData,
			method: "GET",
			dataType: 'json',
			header: {
               'app-id' : 'wx70aa26b5d0445642',
				'content-type': 'application/json',
				'lang' : lang,
				'token': token
			},
			success: (res) => {
				if (res.statusCode === 200) {
						 let data = null;
						 data = res.data;
						 if(typeof data!=='object'){
							 return false;    
						 }	
						if(data.code == 401){
							uni.showToast({
								title : data.msg,
								duration:1500,
								mask:false,
								icon:'none'
							});
							uni.$emit('auto_logout'); 
						}							   
						 resolve(data)	
				} else {
					resolve(res.data)
				}
			},
			error: function(e) {
				//uni.hideLoading();
				// reject('网络出错');
			}
		});
	});
	return promise;
}
//post请求封装
function postRequest(url, data,tips='加载中') {
	// uni.showLoading({
	// 	title: tips
	// });
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: commoneUrl + url,
			data: postData,
			method: 'POST',
			header: {
				'app-id' : 'wx70aa26b5d0445642',
				'content-type': 'application/x-www-form-urlencoded',
				'lang' :   uni.getLocale() || 'en',
				'token': uni.getStorageSync('token') ? uni.getStorageSync('token') : ''
			},
			success: (res) => {
				//uni.hideLoading();
					let data = null;
					 data = res.data;
					 if(typeof data!=='object'){
						 return false;    
					 }
					if(data.code == 401){
						uni.showToast({
							title : data.msg,
							duration:1500,
							mask:false,
							icon:'none'
						});
						uni.$emit('auto_logout');  
					}		
					resolve(data)
			},
			error: function(e) {
				//uni.hideLoading();
				//reject('网络出错');
			}, 
		})
	});
	return promise;
}

function postRequest1(url, data,tips='加载中') {
	// uni.showLoading({
	// 	title: tips
	// });
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.request({
			url: commoneUrl + url,
			data: postData,
			method: 'POST',
			header: {
				'app-id' : 'wx70aa26b5d0445642',
				'content-type': 'application/json',
				'lang' :   uni.getLocale() || 'en',
				'token': uni.getStorageSync('token') ? uni.getStorageSync('token') : ''
			},
			success: (res) => {
				//uni.hideLoading();
					let data = null;
					 data = res.data;
					 if(typeof data!=='object'){
						 return false;    
					 }
					 if(data.code == 401){
					 	uni.showToast({
					 		title : data.msg,
							duration:1500,
							mask:false,
							icon:'none'
						});
						uni.$emit('auto_logout'); 
					 }	  
					resolve(data)				  
				
			},
			error: function(e) {
				//uni.hideLoading();
				// reject('网络出错');
			}
		})
	});
	return promise;
}

//上传接口封装
function uploadFileRequest(url, data) {
	uni.showLoading();
	var promise = new Promise((resolve, reject) => {
		var postData = data;
		uni.uploadFile({
			url: commoneUrl + url,
			filePath: postData,
			header: {
				'app-id' : 'wx70aa26b5d0445642',
				//'content-type': 'application/json',
				'lang' :   uni.getLocale() || 'en',
				'token': uni.getStorageSync('token') ? uni.getStorageSync('token') : ''
			},
			formData: {},
			name: 'file', // 后端接收的文件名
			complete: res => {
				uni.hideLoading();
				resolve(res.data)
			},
			error: function(e) {
				uni.hideLoading();
				// reject('网络出错');
			}
		});
	});
	return promise;
}

module.exports = {
	async_req: function(url, params,method = 'GET',tips='加载中') {
		if(method == 'GET'){
			return getRequest(url, params,tips)
		}else if(method == 'POST'){
			return postRequest(url, params,tips)
		}else{
			return postRequest1(url, params,tips)
		}
	},
	sync_req,
	postRequest,
	getRequest,
	uploadFileRequest
}
